/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Barang.java
 *
 * Created on 30 Nov 10, 10:58:09
 */
package view;

import controller.ControllerSuplier;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.border.BevelBorder;
import utility.TableGenerator;

/**
 *
 * @author Mirza
 */
public class Suplier extends javax.swing.JPanel {

    private int selectedIndex = 0;
    private boolean tambahSuplier = false;
    private Paint paint;

    /** Creates new form Barang */
    public Suplier() {
        initComponents();
        refreshTabel();
        setActiveInput(false);
        setOpaque(false);
        setBorder(new BevelBorder(BevelBorder.LOWERED));
        jButtonHapus.setVisible(false);
    }

    @Override
    protected void paintComponent(Graphics g) {
        Graphics2D gd = (Graphics2D) g.create();
        paint = new GradientPaint(getWidth(), 0, Color.BLUE, 0, 0, Color.WHITE);
        gd.setPaint(paint);
        gd.fillRect(0, 0, getWidth(), getHeight());
        gd.dispose();
        super.paintComponent(g);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTextFieldPencarian = new javax.swing.JTextField();
        jComboBoxJenisPencarian = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jTextFieldNamaSuplier = new javax.swing.JTextField();
        jTextFieldAlamat = new javax.swing.JTextField();
        jTextFieldContactPerson = new javax.swing.JTextField();
        jButtonBatal = new javax.swing.JButton();
        jButtonSimpan = new javax.swing.JButton();
        jButtonHapus = new javax.swing.JButton();
        jButtonUbah = new javax.swing.JButton();
        jButtonTambah = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTableSuplier = new javax.swing.JTable();
        jButton_pilhSuplier = new javax.swing.JButton();

        jTextFieldPencarian.setText("search");
        jTextFieldPencarian.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldPencarianActionPerformed(evt);
            }
        });
        jTextFieldPencarian.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jTextFieldPencarianFocusGained(evt);
            }
        });

        jComboBoxJenisPencarian.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Nama Suplier", "Alamat", "Contact Person" }));
        jComboBoxJenisPencarian.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBoxJenisPencarianActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Verdana", 1, 12));
        jLabel1.setText("Nama Suplier");

        jLabel2.setFont(new java.awt.Font("Verdana", 1, 12));
        jLabel2.setText("Alamat");

        jLabel3.setFont(new java.awt.Font("Verdana", 1, 12));
        jLabel3.setText("Contact Person");

        jTextFieldAlamat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldAlamatActionPerformed(evt);
            }
        });

        jButtonBatal.setText("Batal");
        jButtonBatal.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonBatalActionPerformed(evt);
            }
        });

        jButtonSimpan.setText("Simpan");
        jButtonSimpan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSimpanActionPerformed(evt);
            }
        });

        jButtonHapus.setText("Hapus");
        jButtonHapus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonHapusActionPerformed(evt);
            }
        });

        jButtonUbah.setText("Ubah");
        jButtonUbah.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonUbahActionPerformed(evt);
            }
        });

        jButtonTambah.setText("Tambah");
        jButtonTambah.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonTambahActionPerformed(evt);
            }
        });

        jTableSuplier.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null}
            },
            new String [] {
                "KODE SUPLIER", "NAMA SUPLIER", "ALAMAT", "CONTACT PERSON"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                true, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTableSuplier.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTableSuplierMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(jTableSuplier);
        jTableSuplier.getColumnModel().getColumn(0).setResizable(false);
        jTableSuplier.getColumnModel().getColumn(1).setResizable(false);
        jTableSuplier.getColumnModel().getColumn(2).setResizable(false);
        jTableSuplier.getColumnModel().getColumn(3).setResizable(false);

        jButton_pilhSuplier.setText("Pilih");
        jButton_pilhSuplier.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_pilhSuplierActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 705, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButtonTambah)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonUbah, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonHapus, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 406, Short.MAX_VALUE)
                        .addComponent(jButton_pilhSuplier, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel3)
                            .addComponent(jLabel2))
                        .addGap(56, 56, 56)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jTextFieldNamaSuplier)
                            .addComponent(jTextFieldContactPerson)
                            .addGroup(layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButtonSimpan, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButtonBatal, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jTextFieldAlamat, javax.swing.GroupLayout.PREFERRED_SIZE, 175, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jComboBoxJenisPencarian, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextFieldPencarian, javax.swing.GroupLayout.PREFERRED_SIZE, 183, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldPencarian, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jComboBoxJenisPencarian, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(11, 11, 11)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 337, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonHapus)
                    .addComponent(jButtonUbah)
                    .addComponent(jButtonTambah)
                    .addComponent(jButton_pilhSuplier))
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jTextFieldNamaSuplier, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextFieldAlamat, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextFieldContactPerson, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3)))
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonSimpan)
                    .addComponent(jButtonBatal))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void refreshTabel() {
        try {
            String[] listField = {"KODE_SUPLIER", "NAMA_SUPLIER", "ALAMAT", "CONTACT_PERSON"};
            TableGenerator.generateTable(jTableSuplier, ControllerSuplier.searchSuplier(""), listField);

        } catch (SQLException ex) {
            Logger.getLogger(Suplier.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void setActiveInput(boolean status) {
        jButtonTambah.setEnabled(!status);
        jButtonUbah.setEnabled(!status);
        jButtonHapus.setEnabled(!status);
        jTextFieldNamaSuplier.setEnabled(status);
        jTextFieldAlamat.setEnabled(status);
        jTextFieldContactPerson.setEnabled(status);
        jButtonSimpan.setEnabled(status);
        jButtonBatal.setEnabled(status);
    }

    private void kosongkanInput() {
        jTextFieldNamaSuplier.setText("");
        jTextFieldAlamat.setText("");
        jTextFieldContactPerson.setText("");
    }

    private void tampilkanDataTabelKeInput() {
        jTextFieldNamaSuplier.setText((String) jTableSuplier.getValueAt(selectedIndex, 1));
        jTextFieldAlamat.setText((String) jTableSuplier.getValueAt(selectedIndex, 2));
        jTextFieldContactPerson.setText((String) jTableSuplier.getValueAt(selectedIndex, 3));
    }

    private void jButtonHapusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonHapusActionPerformed
        int a = JOptionPane.showConfirmDialog(this, "Apakah anda yakin akan menghapus data ?");
        if (a == 0) {
            ControllerSuplier.deleteFromSuplier((String) jTableSuplier.getValueAt(selectedIndex, 0));
            refreshTabel();
        }
    }//GEN-LAST:event_jButtonHapusActionPerformed

    private void jTextFieldAlamatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldAlamatActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_jTextFieldAlamatActionPerformed

    private void jButtonSimpanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSimpanActionPerformed
        if (tambahSuplier) {
            if (ControllerSuplier.insertIntoSuplier(jTextFieldNamaSuplier.getText(), jTextFieldAlamat.getText(), jTextFieldContactPerson.getText())) {
                JOptionPane.showMessageDialog(this, "Data Berhasil dimasukkan");
                refreshTabel();
            }
        } else {
            if (ControllerSuplier.updateSuplier((String) jTableSuplier.getValueAt(selectedIndex, 0), jTextFieldNamaSuplier.getText(), jTextFieldAlamat.getText(), jTextFieldContactPerson.getText())) {
                JOptionPane.showMessageDialog(this, "Data Berhasil diubah");
                refreshTabel();
            }
        }
        setActiveInput(false);
    }//GEN-LAST:event_jButtonSimpanActionPerformed

    private void jButtonTambahActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonTambahActionPerformed
        // TODO add your handling code here:
        kosongkanInput();
        tambahSuplier = true;
        setActiveInput(true);
    }//GEN-LAST:event_jButtonTambahActionPerformed

    private void jTableSuplierMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTableSuplierMouseClicked
        // TODO add your handling code here:
        selectedIndex = jTableSuplier.getSelectedRow();
        tampilkanDataTabelKeInput();
        tambahSuplier = false;
    }//GEN-LAST:event_jTableSuplierMouseClicked

    private void jTextFieldPencarianActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldPencarianActionPerformed
        try {
            // TODO add your handling code here:
            String[] listField = {"KODE_SUPLIER", "NAMA_SUPLIER", "ALAMAT", "CONTACT_PERSON"};
            String filter = "";
            if (!jTextFieldPencarian.getText().equals("")) {
                filter = "where " + jComboBoxJenisPencarian.getItemAt(jComboBoxJenisPencarian.getSelectedIndex()).toString().replaceAll(" ", "_") + " like '%" + jTextFieldPencarian.getText() + "%'";
            }
            TableGenerator.generateTable(jTableSuplier, ControllerSuplier.searchSuplier(filter), listField);
        } catch (SQLException ex) {
            Logger.getLogger(Suplier.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jTextFieldPencarianActionPerformed

    private void jTextFieldPencarianFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextFieldPencarianFocusGained
        // TODO add your handling code here:
        jTextFieldPencarian.setText("");
    }//GEN-LAST:event_jTextFieldPencarianFocusGained

    private void jButtonUbahActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonUbahActionPerformed
        tambahSuplier = false;
        setActiveInput(true);
    }//GEN-LAST:event_jButtonUbahActionPerformed

    private void jButtonBatalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonBatalActionPerformed
        kosongkanInput();
        tambahSuplier = true;
        setActiveInput(false);
    }//GEN-LAST:event_jButtonBatalActionPerformed

    private void jComboBoxJenisPencarianActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBoxJenisPencarianActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jComboBoxJenisPencarianActionPerformed

    private void jButton_pilhSuplierActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_pilhSuplierActionPerformed
        ViewUtama.kodeSuplier = (String) jTableSuplier.getValueAt(selectedIndex, 0);
        ViewUtama.formSuplier.setVisible(false);
}//GEN-LAST:event_jButton_pilhSuplierActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonBatal;
    private javax.swing.JButton jButtonHapus;
    private javax.swing.JButton jButtonSimpan;
    private javax.swing.JButton jButtonTambah;
    private javax.swing.JButton jButtonUbah;
    private javax.swing.JButton jButton_pilhSuplier;
    private javax.swing.JComboBox jComboBoxJenisPencarian;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTableSuplier;
    private javax.swing.JTextField jTextFieldAlamat;
    private javax.swing.JTextField jTextFieldContactPerson;
    private javax.swing.JTextField jTextFieldNamaSuplier;
    private javax.swing.JTextField jTextFieldPencarian;
    // End of variables declaration//GEN-END:variables
}
